#include<stdio.h>
#include<stdlib.h>

typedef struct DLNode{
	int data;
	struct DLNode *prior, *next;
}DLNode;

int initiate(DLNode **link){
	if((*link = (DLNode*) malloc(sizeof(DLNode))) == NULL)
		return 0;
	(*link)->next = NULL;
	(*link)->prior = NULL;
	return 1;
}

int insert(DLNode *h, int i, int data){
	int count = 0;
	DLNode *p, *new;
	p = h;
	++count;
	while(count != i && p->next != NULL){
		p = p->next;
		++count;
	}
	if(count !=i && p->next == NULL && i != 0){
		return 0;
	}

	if((new = (DLNode*) malloc(sizeof(DLNode))) == NULL)
		return 0;
	new->data = data;
	new->prior = p;
	new->next = p->next;
	if(i == 0 || (i == 1 && p->next == NULL)){
      		p->next = new;
	}else{
		p->next->prior = new;
		p->next = new;
	}

	return 1;
}

int print(DLNode *h){
	DLNode *p = h->next;

	while(p != NULL){
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
	return 1;
}

int main(){
	DLNode **link, *head;
	link = &head;
	if(!initiate(link))
		printf("initiate failed\n");
	int data = 0;
	do{
		printf("Enter the data of the linked list:");
		scanf("%d", &data);
		if(data != 0)
			insert(head, 0, data);
	}while(data != 0);

	print(head);
}
